package org.soulraven.games.memory.ui.game;

import java.util.ArrayList;
import java.util.List;

import org.soulraven.games.memory.model.Card;
import org.soulraven.games.memory.model.GameDifficulty;
import org.soulraven.games.memory.ui.game.controller.SelectionController;

public final class CardsGridBuilder {

	private CardsGridBuilder() {
	}

	public static ButtonCard[][] build(final GameDifficulty difficulty, final SelectionController selectionController) {
		// TODO build according to game difficulty
		ButtonCard[][] cards = new ButtonCard[4][4];
		List<Integer> numbers = new ArrayList<Integer>();
		ButtonCard buttonCard = null;
		int index;
		int number;

		for (int i = 0; i < 8; i++) {
			numbers.add(i);
			numbers.add(i);
		}
		for (int i = 0; i < cards.length; i++) {
			for (int j = 0; j < cards[1].length; j++) {
				index = (int)(Math.random() * numbers.size());
				number = numbers.get(index);
				numbers.remove(index);
				buttonCard = new ButtonCard(new Card(number));
				buttonCard.addActionListener(selectionController);
				cards[i][j] = buttonCard;
			}
		}
		return cards;
	}
}
